<?php
class page extends spController
{
	function index(){ // 页面管理
		$this->tpl_title = "页面管理";  
		$this->results = spClass("lib_contents")->spPager($this->spArgs('page', 1), 8)->findAll(array("type" => 'page'),"id DESC"); 
		$this->page_pager = spClass("lib_contents")->spPager()->getPager();
		$this->display("admin/page_index.html");
	}
	
	function write(){ // 撰写页面
		$this->tpl_title = "撰写新页面";
		$this->formUrl = spUrl("page", "add");
		$this->display("admin/page_write.html");
	}
	
	function add(){ // 添加页面
		$user = spClass("lib_contents");
		$newrow = array( // 这里制作新增记录的值
			'title' => $this->spArgs('title'), 
			'slug' => $this->spArgs('slug'),
			'created' => date("Y-m-d H:i:s"),
			'text' => $this->spArgs('text'),
			'thumb' => $this->spArgs('thumb'),
			'overview' => $this->spArgs('overview'),
			'author' => $_SESSION["userinfo"]["name"],
			'type' => 'page',
		);
		$user->create($newrow); 
		$this->success("页面撰写成功！", spUrl("page", "index"));
	}

	function edit(){ // 页面修改
		$this->tpl_title = "编辑页面";
		$this->formUrl = spUrl("page", "update");
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
			// 查找表，获取信息
			$this->info = spClass("lib_contents")->find(array('id'=>$id));
			$this->display("admin/page_write.html");
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("page","index"));
		}
	}	

	function update(){ //更新
		$post = spClass("lib_contents");
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
			// 执行删除
		$conditions = array("id"=>$id); 
		$newrow = array(
			'title' => $this->spArgs('title'), 
			'thumb' => $this->spArgs('thumb'),
			'modified' => date("Y-m-d H:i:s"),
			'text' => $this->spArgs('text'),
			'overview' => $this->spArgs('overview'),
			'author' => $_SESSION["userinfo"]["name"],
			);
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新页面成功！", spUrl("page", "index"));
		}
	}

	function check(){
		// 接收提交的slug值
		$slug = $this->spArgs('slug');
		$results = spClass('lib_contents')->find(array( 'slug' => $slug )); // 查找
		$slugs = $results['slug'];

		if( $slug == $slugs ){
			$result = array(
				'status' => 0, // 失败标志
				'message' => '<span class="input-notification error png_bg">缩略名重复</span>', // 提示信息
			);		
		}else{
			$result = array(
				'status' => 1, // 成功标志
				'message' => '<span class="input-notification success png_bg">缩略名可用</span>', // 提示信息
			);	
		}
		echo json_encode( $result ); // 返回（显示）JSON结果
	}
	
	function del(){// 删除页面
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
			// 执行删除
			spClass("lib_contents")->delete(array('id'=>$id));
			$this->success("删除成功！", spUrl("page","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("page","index"));
		}
	}	
	
}